home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 11 / FM Towns Free Software Collection 11.iso / t_os / lib / yotpin / src / pisave.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-01  |  14.2 KB  |  690 lines

  1. /*
  2. *    Yamana's Otomeza Plug-in Tool
  3. *        Pi セーバ
  4. *    
  5. *    1995.08.06    
  6. *    1995.08.27    部分セーブへの対処をあきらめた(~~~)
  7. *    1995.09.01    パターンチェック部のバグ除去
  8. *    
  9. *    問題点
  10. *    ・32ドット単位でしか保存できない。
  11. *      メモリがふんだんにあるか、高速マシンであるか、多少圧縮率を犠牲にする
  12. *      か、このうちどれかを選ばない限り無理。
  13. *    
  14. *    "Pi" (c) Yanagisawa
  15. */
  16.  
  17. #include    "otome_pi.h"
  18.  
  19. const char    longname[]    = "ぱいせーば";
  20. const char    extend[]    = "PI";
  21.  
  22. #define        USE_FILE    PI_SAVE
  23. #include    "otome_pi.c"
  24.  
  25. char    def_comment[]= "PiSAVER/OTOMEZA";
  26.  
  27. typedef
  28.     struct
  29.     {    char    mode;        /* パレットなし= 0x80 */
  30.         char    n;
  31.         char    m;            /* 縦横比 n/m */
  32.         char    pix;        /* 4 or 8 */
  33.         char    name[4];    /* 機種名 */
  34.         short    mdat_len;    /* 機種予約コードサイズ */
  35.         char    *mdat;        /* 機種予約コード */
  36.         short    x;
  37.         short    y;
  38.     }
  39.     PiHeader;
  40.  
  41.  
  42. #define    MAX_PIXPAGESIZE        (32*1024)
  43. int     pixPageSize= MAX_PIXPAGESIZE;
  44. int     pixPage    = 0;
  45.  
  46. int        Pi_countBit = 0;
  47. int        Pi_maxBit   = MAX_PIXPAGESIZE*8 ;
  48. char    *ColTbl;
  49.  
  50. int     precol;
  51. char    **pixel;
  52.  
  53. char    *user    ='\0';
  54. char    *comment='\0';
  55.  
  56. /********************************************************************/
  57.  
  58. int     Pi_getColor256();
  59. int     Pi_getColor16();
  60. int     Pi_getColor256D();
  61. int     Pi_getColor16D();
  62. void    Pi_putColor256();
  63. void    Pi_putColor16();
  64. void    Pi_putPos();
  65. void    Pi_putLen();
  66.  
  67. int     Width;
  68. char    *f2buf;
  69.  
  70. int     Pi_posTable[][2] = 
  71.             {    {-2, 0 },    /* 0 :前のピクセルにより変化するので注意 */
  72.                 { 0,-1 },    /* 1: 1ライン上                */
  73.                 { 0,-2 },    /* 2: 2ライン上                */
  74.                 { 1,-1 },    /* 3: 1ライン上の1ドット右    */
  75.                 {-1,-1 }    /* 4: 1ライン上の1ドット左    */
  76.             };
  77.  
  78. int Pi_encode( head,image )
  79. PiHeader    *head;
  80. char        *image;
  81. {
  82.     int     i;
  83.     int     c0,c1,l[5];
  84.     int     Height,imagemax;
  85.     int     prepos,pos, off, len;
  86.     int     (*Pi_getColor)();
  87.     void    (*Pi_putColor)();
  88.     
  89.     /* 32ドット単位で渡される */
  90.     Width    = (head->x+31) & 0xffe0 ;
  91.     Height   =  head->y ;
  92.     imagemax = Width*Height;
  93.     
  94.     if( (f2buf=(char*)PI_MALLOC( Width*2+2 ))==NULL )
  95.         return PI_ERROR_NO_MEMORY;
  96.     
  97.     if( head->pix == 8 )
  98.     {    Pi_getColor    = Pi_getColor256D;
  99.         Pi_putColor    = Pi_putColor256;
  100.     }else
  101.     {    Pi_getColor    = Pi_getColor16D;
  102.         Pi_putColor    = Pi_putColor16;
  103.     }
  104.     for( i=1; i<=4 ; i++ )
  105.         Pi_posTable[i][0] += Pi_posTable[i][1]*Width;
  106.     
  107.     /**************************/
  108.     
  109.     /* (1) 始めの2色で先頭2ラインを埋める */
  110.     c0 = Pi_getColor( 0 );
  111.     for ( i = 0; i<=Width ; i++ )    /* 余分に埋めるのは長さを調べるとき */
  112.         WORD( f2buf+i*2 ) = c0;        /* pos=3,4では1ドットずれているため */
  113.     
  114.     precol = 0;
  115.     Pi_putColor( c0 & 0xff );
  116.     Pi_putColor( c0 >>8    );
  117.     
  118.     /* 一番最初のパターン */
  119.     /* 上2ラインがみんな同じなので一番長いものは分かっている */
  120.     len = 2;
  121.     while( c0 == Pi_getColor(len) && len<imagemax )
  122.         len += 2;
  123.     
  124.     pos = prepos = 2;
  125.     Pi_putPos( pos );
  126.     Pi_putLen( len/2 );
  127.     off = len;
  128.     
  129.     /* 高速化のため最初の2ラインだけは別に処理する */
  130.     while( off< Width*2 )
  131.     {
  132.         pos = Pi_search( &l, off,prepos, imagemax, Pi_getColor );
  133.         
  134.         if( (l[0] | l[1] | l[2] | l[3] | l[4]) == 0 )
  135.         {    Pi_putPos( prepos );
  136.             precol = Pi_getColor( off-1 )& 0xff;
  137.             while(1)
  138.             {    c0 = Pi_getColor( off );    off+=2;
  139.                 Pi_putColor( c0 & 0xff );
  140.                 Pi_putColor( c0 >> 8 );
  141.                 
  142.                 if ( off > imagemax )    break;
  143.                 
  144.                 prepos=(-1);
  145.                 pos = Pi_search( &l, off,prepos, imagemax, Pi_getColor );
  146.                 if( (l[0] | l[1] | l[2] | l[3] | l[4]) )
  147.                     break;
  148.                 Pi_putBit( 1, 1 );
  149.             }
  150.             Pi_putBit( 0,1 );
  151.             if ( off > imagemax )
  152.                 break;
  153.         }
  154.         Pi_putPos( pos );
  155.         Pi_putLen( l[pos] );
  156.         prepos = pos;
  157.         off   += l[pos]*2;
  158.     }
  159.     if( head->pix == 8 )    Pi_getColor    = Pi_getColor256;
  160.                     else    Pi_getColor    = Pi_getColor16;
  161.     
  162.     while( off< imagemax )
  163.     {    
  164.         /* (3)連続している最長パターンを探す */
  165.         pos = Pi_search( &l, off,prepos, imagemax, Pi_getColor );
  166.         
  167.         /* 同じパターンが見つからないとき */
  168.         if( (l[0] | l[1] | l[2] | l[3] | l[4]) == 0 )
  169.         {
  170.             /* (5)前回と同じ位置を記録 */
  171.             Pi_putPos( prepos );
  172.             precol = Pi_getColor( off-1 )& 0xff;
  173.             
  174.             /* (6)2点を記録し注目点移動 */
  175.             while(1)
  176.             {    c0 = Pi_getColor( off );    off+=2;
  177.                 Pi_putColor( c0 & 0xff );
  178.                 Pi_putColor( c0 >> 8 );
  179.                 
  180.                 if ( off > imagemax )
  181.                     break;
  182.                 
  183.                 /* (7)連続しているパターンがあれば終わり */
  184.                 prepos=(-1);
  185.                 pos = Pi_search( &l, off,prepos, imagemax, Pi_getColor );
  186.                 
  187.                 if( (l[0] | l[1] | l[2] | l[3] | l[4]) )
  188.                     break;
  189.                 
  190.                 Pi_putBit( 1, 1 );
  191.             }
  192.             Pi_putBit( 0,1 );    /* 0を書き込む */
  193.             if ( off > imagemax )
  194.                 continue;
  195.             
  196.         }
  197.         /* 同じパターンが見つかったとき */
  198.         /* (4')一番長くなる位置と長さを記録 */
  199.         Pi_putPos( pos );
  200.         Pi_putLen( l[pos] );
  201.         prepos = pos;
  202.         off += l[pos]*2;
  203.     }
  204.     
  205.     Pi_putBit( 0, 32 );
  206.     
  207.     return NOERR;
  208. }
  209.  
  210. /*********************/
  211.  
  212. /* 周囲5点から同じパターンを探す */
  213. int Pi_search( l,  off,prepos, imagemax,getColor )
  214. int     *l,off,imagemax,prepos;
  215. int     (*getColor)();
  216. {
  217.     int     i,len,max;
  218.     int     bufoff;
  219.     
  220.     max = 0;
  221.     l[0]= 0;
  222.     if( prepos!=0 )
  223.     {    
  224.         i = getColor(off-2);
  225.         /* 直前のパターンを比較 */
  226.         if( ((i>>8)& 0x0ff) == (i & 0xff) )    bufoff = off-2;
  227.                                     else    bufoff = off-4;
  228.         len = 0;
  229.         while( getColor(bufoff) == getColor(off+len) )
  230.         {    len     += 2;
  231.             bufoff  += 2;
  232.             if( off+len >= imagemax )    break;
  233.         }
  234.         l[0] = len/2;
  235.     }
  236.     
  237.     /** pos=1~4 **/
  238.     for( i=1; i<5; i++ )
  239.     {    if( prepos == i )
  240.         {    l[i]=0;
  241.             continue;
  242.         }
  243.         bufoff = off + Pi_posTable[i][0];
  244.         len    = 0;
  245.         while( getColor( off+len )==getColor( bufoff ) )
  246.         {    len     += 2;
  247.             bufoff    += 2;
  248.             
  249.             if( off+len >= imagemax )    break;
  250.         }
  251.         l[i] = len/2 ;
  252.         if( l[max] < l[i] )    max = i;
  253.     }
  254. //    for(i=0;i<5;i++)    printf("l[%d]=%d,",i,l[i] );printf("\r\n");
  255.     
  256.     return max;
  257. }
  258.  
  259. /*********************/
  260.  
  261. int Pi_getColor256D( off )
  262. int off;
  263. {
  264.     if( off < 0 )    return WORD( f2buf+ Width*2 + off );
  265.     
  266.     return WORD( PI_IMAGEBUF + off );
  267. }
  268.  
  269. int Pi_getColor16D( off )
  270. int off;
  271. {
  272.     if( off < 0 )    return WORD( f2buf+ Width*2 + off );
  273.     
  274.     if( off & 1 )
  275.     {    off >>= 1;
  276.         return  ((PI_IMAGEBUF[off  ] & 0xf0 )>>4)
  277.               | ((PI_IMAGEBUF[off+1] & 0x0f )*256);
  278.     }else
  279.     {    off >>= 1;
  280.         return  ((PI_IMAGEBUF[off  ] & 0x0f )    )
  281.               | ((PI_IMAGEBUF[off  ] & 0xf0 )<<4);
  282.     }
  283. }
  284.  
  285. int Pi_getColor256( off )
  286. int off;
  287. {
  288.     return WORD( PI_IMAGEBUF + off );
  289. }
  290.  
  291. int Pi_getColor16( off )
  292. int off;
  293. {
  294.     if( off & 1 )
  295.     {    off >>= 1;
  296.         return  ((PI_IMAGEBUF[off  ] & 0xf0 )>>4)
  297.               | ((PI_IMAGEBUF[off+1] & 0x0f )*256);
  298.     }else
  299.     {    off >>= 1;
  300.         return  ((PI_IMAGEBUF[off  ] & 0x0f )    )
  301.               | ((PI_IMAGEBUF[off  ] & 0xf0 )<<4);
  302.     }
  303. }
  304.  
  305. /*********************/
  306.  
  307. #define    chk_bufpage()    \
  308.         if( Pi_countBit >= Pi_maxBit )\
  309.         {    if( remallocPixBuf() )    return ERROR;\
  310.             Pi_countBit = 0;\
  311.         }
  312.  
  313. /* 任意ビット長のデータを格納する */
  314. int     Pi_putBit( dat,bit )
  315. int     dat,bit;
  316. {
  317.     char    restBit[]= {    0x00,0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff };
  318.     char    usedBit[]= {    0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe };
  319.     int     rest;
  320.     char    *p;
  321.     
  322.     chk_bufpage();
  323.     
  324.     rest = 8-(Pi_countBit & 7);
  325.     while( rest<bit )
  326.     {    
  327.         p = &pixel[pixPage][ (Pi_countBit>>3) ];
  328.         
  329.         *p &= usedBit[ (Pi_countBit & 7) ];
  330.         *p |= ((dat>>(bit - rest)) & restBit[rest]);
  331.         bit -= rest;
  332.         Pi_countBit += rest;
  333.         chk_bufpage();
  334.         
  335.         rest = 8-(Pi_countBit & 7);
  336.     }
  337.     if( rest>=bit )
  338.     {
  339.         p = &pixel[pixPage][ (Pi_countBit>>3) ];
  340.         
  341.         *p &= usedBit[ (Pi_countBit & 7) ];
  342.         *p |= ((dat & restBit[bit])<<(rest-bit));
  343.         
  344.         Pi_countBit += bit;
  345.     }
  346.     
  347.     return NOERR;
  348. }
  349.  
  350.  
  351. void    Pi_putPos( pos )
  352. int pos;
  353. {
  354.     switch( pos )
  355.     {
  356.     case 0:        Pi_putBit( 0x00, 2 );    break;
  357.     case 1:        Pi_putBit( 0x01, 2 );    break;
  358.     case 2:        Pi_putBit( 0x02, 2 );    break;
  359.     case 3:        Pi_putBit( 0x06, 3 );    break;
  360.     case 4:        Pi_putBit( 0x07, 3 );    break;
  361.     }
  362. }
  363.  
  364. void    Pi_putLen( len )
  365. unsigned int     len;
  366. {
  367.     unsigned int i;
  368.     
  369.     i=1;
  370.     while( (len>>i) )    i++;
  371.     
  372.     Pi_putBit( 0xfffffffe, i );
  373.     if( i==1 )    return;
  374.     
  375.     Pi_putBit( len, i-1 );
  376. }
  377.  
  378. /*************************/
  379.  
  380. void    Pi_initColorTable(col)
  381. int     col;
  382. {
  383.     int     i, j;
  384.     
  385.     for( i=0; i<col; i++ )
  386.         for( j=0; j<col; j++ )
  387.             ColTbl[ j + i*col ] = ((i + col-j) & (col-1));
  388.     
  389. }
  390.  
  391. void    Pi_putColor16( col )
  392. int     col;
  393. {
  394.     int     i,j,k;
  395.     char    *p;
  396.     
  397.     p = ColTbl + precol*16 ;
  398.     j = p[0];
  399.     for( i=1; j!=col ; i++ )
  400.     {    k    = p[i];
  401.         p[i] = j;
  402.         j    = k;
  403.     }
  404.     p[0]   = col;
  405.     precol = col;
  406.     i--;
  407.          if( i<2 )    Pi_putBit( 1, 1 ),Pi_putBit( i, 1 );
  408.     else if( i<4 )    Pi_putBit( 0, 2 ),Pi_putBit( i, 1 );
  409.     else if( i<8 )    Pi_putBit( 2, 3 ),Pi_putBit( i, 2 );
  410.     else            Pi_putBit( 3, 3 ),Pi_putBit( i, 3 );
  411.     
  412. }
  413. void    Pi_putColor256( col )
  414. int     col;
  415. {
  416.     int     i,j,k;
  417.     char    *p;
  418.     
  419.     p = ColTbl + precol*256 ;
  420.     j = p[0];
  421.     for( i=1; j!=col ; i++ )
  422.     {    k    = p[i];
  423.         p[i] = j;
  424.         j    = k;
  425.     }
  426.     p[0]   = col;
  427.     precol = col;
  428.     i--;
  429.          if( i<  2 )    Pi_putBit( 0x01, 1 ),Pi_putBit( i, 1 );
  430.     else if( i<  4 )    Pi_putBit( 0x00, 2 ),Pi_putBit( i, 1 );
  431.     else if( i<  8 )    Pi_putBit( 0x02, 3 ),Pi_putBit( i, 2 );
  432.     else if( i< 16 )    Pi_putBit( 0x06, 4 ),Pi_putBit( i, 3 );
  433.     else if( i< 32 )    Pi_putBit( 0x0e, 5 ),Pi_putBit( i, 4 );
  434.     else if( i< 64 )    Pi_putBit( 0x1e, 6 ),Pi_putBit( i, 5 );
  435.     else if( i<128 )    Pi_putBit( 0x3e, 7 ),Pi_putBit( i, 6 );
  436.     else                Pi_putBit( 0x3f, 7 ),Pi_putBit( i, 7 );
  437.     
  438. }
  439.  
  440. /********************************************************/
  441. void freePixBuf()
  442. {
  443.     int i;
  444.     
  445.     for( i=0; i<=pixPage; i++ )
  446.         PI_FREE( pixel[i] );
  447.     PI_FREE( pixel );
  448. }
  449.  
  450. int mallocPixBuf( Width,Height )
  451. int     Width,Height;
  452. {
  453.     int     all,page;
  454.     
  455.     all  = Width * Height ;                /* 全画面データに必要なメモリ */
  456.     page = (all / pixPageSize )+1 ;        /* 最大ページサイズ */
  457.     
  458.     if( (pixel=(char**)PI_MALLOC( sizeof(char*)*page ))==NULL )
  459.         return ERROR;
  460.     
  461.     /* 1ページだけ確保 */
  462.     if( (pixel[0] = (char*)PI_MALLOC( pixPageSize + 2 ))==NULL )
  463.     {    free( pixel );
  464.         return ERROR;
  465.     }
  466.     
  467.     return NOERR;
  468. }
  469.  
  470. int remallocPixBuf()
  471. {
  472.     if( (pixel[pixPage+1] = (char*)PI_MALLOC( pixPageSize + 2 ))==NULL )
  473.     {    freePixBuf();
  474.         return ERROR;
  475.     }
  476.     pixPage++;
  477.     return NOERR;
  478. }
  479.  
  480. /*************************************************************/
  481. /*    乙女座から得たコメントを分解する    */
  482.  
  483. #define    iskanji(c)    ( (0x81<=(c) && (c)<=0x9f) || (0xe0<=(c) && (c)<=0xfc))
  484. #define    iskanji2(c)    ( (0x40<=(c) && (c)<=0x7e) || (0x80<=(c) && (c)<=0xfc))
  485.  
  486. int Pi_setComment()
  487. {
  488.     int     i,len;
  489.     
  490.     if( PI_COMMENT==NULL || (len=strlen( PI_COMMENT ))<1 )
  491.         return NOERR;
  492.     
  493.     /* 面倒なのでメモリチェックは無視。これくらいで失敗するかな?? */
  494.     
  495.     for( i=0; i<len ; i++ )
  496.     {    if( PI_COMMENT[i]==',' )
  497.         {    user=PI_MALLOC( i+1 );
  498.             strncpy( user, PI_COMMENT, i ),user[i]='\0';
  499.             
  500.             for( i++;i<len; i++ )
  501.                 if( PI_COMMENT[i]!=' ' && PI_COMMENT[i]!='\t' )    break;
  502.             if( i<len )
  503.             {    comment=PI_MALLOC( len-i+2 );
  504.                 strcpy( comment, PI_COMMENT+i );
  505.             }
  506.             return NOERR;
  507.         }
  508.     }
  509.     user=PI_MALLOC( len+1 );
  510.     strcpy( user, PI_COMMENT );
  511.     
  512.     return NOERR;
  513. }
  514.  
  515. #define    ENV_MAX        80
  516.  
  517. char    *my_getenv( name )
  518. char    *name;
  519. {
  520.     char    envbuf[ ENV_MAX+1 ];
  521.     int     i;
  522.     
  523.     /* '='で始まって末尾に '\\' を追加したものが返される */
  524.     if( (i=TOS_getPath( envbuf, name, strlen(name) ))==0 )
  525.         return NULL;
  526.     
  527.     if( i>2 )
  528.     {    if( !iskanji( envbuf[i-2] ) )    /* 漢字コード2バイト目でない */
  529.             envbuf[i-1] = '\0';
  530.     }
  531.     else if( i>0 )
  532.         envbuf[i-1] = '\0';
  533.     
  534.     if( i<1 || (user = PI_MALLOC( strlen( &envbuf[1] )+1 ))==NULL )
  535.         return '\0';
  536.     
  537.     strcpy( user, &envbuf[1] );        /* '=' のぶんスキップ */
  538.     
  539.     return user;
  540. }
  541.  
  542. /********************************************************/
  543.  
  544. int Pi_writeBuffers( fp,head,palette )
  545. FILE        *fp;
  546. PiHeader    *head;
  547. char        *palette;
  548. {
  549.     int     i;
  550.     char    *p;
  551.     
  552.     /****/
  553.     if( 2 != fwrite( "Pi", 1,2, fp ) )
  554.         return ERROR;
  555.     
  556.     p = '\0';
  557.     if( user != '\0' )    p = user;
  558.                 else    p = my_getenv("USER");
  559.     if( p != '\0' )
  560.     {    fwrite( "User:", 1,5, fp );
  561.         fwrite( p, 1, strlen(p), fp );
  562.     }
  563.     if( comment == '\0' )    p = def_comment;
  564.                     else    p = comment;
  565.     fwrite( "\r\n", 1, 2, fp);
  566.     fwrite( p, 1, strlen(p), fp);
  567.     fwrite( "\x1a\0", 1, 2, fp);
  568.     
  569.     /****/
  570.     fwrite( head,            1, 4, fp );
  571.     fwrite( head->name,        1, 4, fp );
  572.     fputc ( head->mdat_len>>8    , fp );
  573.     fputc ( head->mdat_len        , fp );
  574.     if( head->mdat_len>0 )    fwrite( head->mdat, 1, head->mdat_len, fp );
  575.     fputc ( head->x >>8            , fp );
  576.     fputc ( head->x             , fp );
  577.     fputc ( head->y >>8            , fp );
  578.     fputc ( head->y             , fp );
  579.     
  580.     fwrite( palette, 1, (1<<head->pix)*3 ,fp);
  581.     
  582.     /****/
  583.     for( i=0; i<pixPage; i++ )
  584.         if( pixPageSize != fwrite( &pixel[i][0], 1, pixPageSize, fp ) )
  585.             return ERROR;
  586.     if( (i = ((Pi_countBit+7)>>3)) >0 )
  587.         if( i != fwrite( &pixel[pixPage][0], 1, i, fp ) )
  588.             return ERROR;
  589.     
  590.     
  591.     return NOERR;
  592. }
  593.  
  594. void Pi_head( head,palette,size,pal )
  595. char        *palette;
  596. PiHeader    *head;
  597. POINT        *size;
  598. char        *pal;
  599. {
  600.     int     i,j,col;
  601.     
  602.     head->mode    = 0;
  603.     head->n        = 1;
  604.     head->m        = 1;
  605.     head->pix    = pi_imge->pix;
  606.     
  607.     strncpy( head->name, "TOWN",4 );
  608.     
  609.     head->mdat_len = 0;
  610.     head->x = (size->x+31)& 0xffe0 ;    /* う~む。仕方がない。*/
  611.     head->y = size->y;
  612.     
  613.     /*** パレットデータ ***/
  614.     
  615.     col = (1 << head->pix);
  616.     for( i=0,j=8; i<col; i++,j+=8 )
  617.     {
  618.         palette[ i*3   ] = pal[ j+1 ] ;        /* R */
  619.         palette[ i*3+1 ] = pal[ j+2 ] ;        /* G */
  620.         palette[ i*3+2 ] = pal[ j   ] ;     /* B */
  621.     }
  622.     
  623. }
  624.  
  625. int Pi_save( fp, buf, size, pix, pal )
  626. FILE    *fp;
  627. char    *buf,*pal;
  628. int     pix;
  629. POINT    *size;
  630. {
  631.     PiHeader    head;
  632.     int         col,ret;
  633.     char        *palette;
  634.     
  635.     if( pix ==8 )    col = 256;
  636.             else    col = 16;
  637.     
  638.     if( (palette=(char*)PI_MALLOC( col*3 + col*col ))==NULL )
  639.         return PI_ERROR_NO_MEMORY;
  640.     ColTbl  = palette  + col*3 ;
  641.     
  642.     if( mallocPixBuf( PI_SIZE_X,PI_SIZE_Y ) )
  643.     {    PI_FREE(palette);
  644.         return PI_ERROR_NO_MEMORY;
  645.     }
  646.     
  647.     /*** ヘッダ作成 ***/
  648.     Pi_head( &head, palette, &PI_SIZE, PI_COLMAP );
  649.     
  650.     /*** 圧縮 ***/
  651.     Pi_initColorTable( col );
  652.     ret=Pi_encode( &head, buf );
  653.     ret = 0;
  654.     
  655.     /*** 書き出し ***/
  656.     if( ret==NOERR )
  657.         ret=Pi_writeBuffers( fp, &head, palette );
  658.     
  659.     
  660.     PI_FREE( palette );
  661.     freePixBuf();
  662.     
  663.     return ret;
  664. }
  665.  
  666. /*******************************************************************/
  667. /**  プラグイン呼び出し **/
  668.  
  669. int APL_exec()
  670. {
  671.     FILE    *fp;
  672.     int     ret;
  673.     
  674.     if( PI_PIX >8 )    return ERROR;
  675.     
  676.     Pi_setComment();
  677.     
  678.     if( (fp = fopen( PI_FNAME , "wb" )) == NULL )
  679.         return PI_ERROR_FILE_OPEN;
  680.     
  681.     ret=Pi_save( fp, PI_IMAGEBUF, &PI_SIZE, PI_PIX, PI_COLMAP );
  682.     fclose( fp );
  683.     if( ret )    remove( PI_FNAME );
  684.     
  685.     if( user    != '\0' )    PI_FREE( user );
  686.     if( comment != '\0' )    PI_FREE( comment );
  687.     return ret;
  688. }
  689.  
  690.